package com.ykt.common.thirdapi.polyv;

import com.alibaba.fastjson.TypeReference;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.ykt.common.thirdapi.polyv.model.PolyvVideo;
import com.ykt.common.thirdapi.polyv.model.PolyvVo;
import com.ykt.common.utils.HTTPClientUtil;
import com.ykt.common.utils.JsonConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保利威视视频同步
 * @author long.hua
 * @version 1.0.0
 * @since 2016-05-26 22:41
 */
public class PolyvHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(PolyvHelper.class);

    private Prop prop = PropKit.use("polyv.properties");
    private String url = prop.get("polyv.url");
    private String userId = prop.get("polyv.userId"); // 获取服务器时间请求地址
    private String writetoken = prop.get("polyv.writetoken");
    private String readtoken = prop.get("polyv.readtoken");
    private String secretkey = prop.get("polyv.secretkey");


    public List<PolyvVideo> getNewList(int pageNum, int numPerPage){
        Map<String, Object> paramMap=new HashMap<>();

        paramMap.put("readtoken", readtoken);
        paramMap.put("pageNum", pageNum);
        paramMap.put("numPerPage", numPerPage);
        paramMap.put("readtoken", readtoken);

        String result =  HTTPClientUtil.sendPostRequest(url, paramMap);
        if(result == null || result.isEmpty()){
            LOGGER.info("Sync video is empty, request param: {}",JsonConverter.format(paramMap));
            return null;
        }

        try{
            PolyvVo<List<PolyvVideo>> polyvVo = JsonConverter.parse(result, new TypeReference<PolyvVo<List<PolyvVideo>>>(){});
            return polyvVo.getData();
        }catch (Exception e){
            LOGGER.error("Sync video is error, request param: {}", e, JsonConverter.format(paramMap));
            return null;
        }
    }

}
